package algo.couterbased;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import algorithm.AbstractCounterBased;

public class Frequent extends AbstractCounterBased {
	
	public Frequent() {
		super("Frequent");
	}

	@Override
	public Map<Object, Double> compute(int[] stream){
		
		size = 0;
		List<Object> t = new ArrayList<Object>();
		result = new HashMap<Object, Double>(); 
		
		for (Object item : stream) {
			size++;
			if (t.contains(item)){
				result.put(item, result.get(item) + 1);
			} else if (t.size() < stream.length - 1){
				t.add(item);
				result.put(item, 1.0);
			} else {
				for (Object j : t) {
					double cj = result.get(j);
					result.put(j, cj - 1);
					if (cj == 0)
						t.remove(j);
				}
			}
		} 
		
		return result;
	}
	

}
